package com.example.AIstudy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.AIstudy.domain.Question;
import com.example.AIstudy.exception.AIstudyException;
import com.example.AIstudy.service.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/question")
@Api(tags = "问题信息管理API")
public class QuestionController {

    @Autowired
    QuestionService questionService;

    @PostMapping("")
    @ApiOperation("新增一条问题记录")
    public void InsertQuestion(@ApiParam("班级信息")@RequestBody Question question) throws AIstudyException {
        questionService.insertQuestion(question);
    }

    @GetMapping("/questioneLevel/{stuID}")
    @ApiOperation("根据学生ID搜素学生的问题水平")
    public Float getLevelByStuID(@ApiParam("学生ID")@PathVariable int stuID) {
        List<Integer> level = questionService.getLevelByStuID(stuID);
        //先对所有的问题求和 然后再求均值
        int size = level.size();
        float sum = 0;
        for (int i = 0; i < size; i++) {
            sum += level.get(i);
        }
        return sum / size;
    }

    @ApiOperation("根据学生的id查询问题记录")
    @GetMapping("")
//    @PreAuthorize("hasAnyAuthority(#studentID.toString())")
    public IPage<Question> findUserByClassID(@ApiParam("学生ID")@RequestParam(value = "studentID") Integer studentID,
                                               @ApiParam("问题")@RequestParam(value = "question",required = false) String question,
                                               @ApiParam("最早提问时间")@RequestParam(value = "minTime",required = false) Timestamp minTime,
                                               @ApiParam("最晚提问时间")@RequestParam(value = "maxTime",required = false) Timestamp maxTime,
                                               @ApiParam("页码")@RequestParam(defaultValue = "0")Integer pageNum,
                                               @ApiParam("每页记录数") @RequestParam(defaultValue = "10")Integer pageSize){
        Map<String,Object> condition=new HashMap<>();
        condition.put("stuID",studentID);
        //教师的id只有在超级管理员登录的时候才为空
        if(question!=null) {
            condition.put("question",question);
        }
        if(minTime!=null) {
            condition.put("minTime",minTime);
        }
        if(maxTime!=null) {
            condition.put("maxTime",maxTime);
        }
        return questionService.findQuestion(condition, pageNum,pageSize);
    }

}
